package com.mxchip.livestar.su;

import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FIleManager {
    private static FileOutputStream fos;
    private static FileOutputStream filenamefos;
    private static boolean isWrite = true;

    public static void create() {
        if (!isWrite) {
            return;
        }
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "receiveHDMI");
        try {
            fos = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    public static void create(String filename) {
        if (!isWrite) {
            return;
        }
        if (filenamefos == null) {
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + filename);
            try {
                filenamefos = new FileOutputStream(file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }

    }

    public static void close() {
        if (!isWrite) {
            return;
        }
        try {
            if (fos != null)
                fos.close();
            if (filenamefos != null)
                filenamefos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        fos = null;
        filenamefos = null;
    }

    public static void write(byte[] data) {
        if (!isWrite) {
            return;
        }
        try {
            if (fos != null) {
                fos.write(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void writeString(String data) {
        if (!isWrite) {
            return;
        }
        try {
            if (fos != null) {
                fos.write(data.getBytes());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void writeOther(byte[] data) {
        if (!isWrite) {
            return;
        }

        try {
            if (filenamefos != null) {
                filenamefos.write(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
